package com.fstech.whiteboadrd.cahrt;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;

import com.fstech.whiteboadrd.R;
import com.fstech.whiteboadrd.utils.UIUtils;

import java.util.Map;
import java.util.Random;

public class ChartProvider {

    static int index = 0;
    static final int[] colors = new int[]{
            UIUtils.getColor(R.color.chart_color1),
            UIUtils.getColor(R.color.chart_color2),
            UIUtils.getColor(R.color.chart_color3),
            UIUtils.getColor(R.color.chart_color4),
            UIUtils.getColor(R.color.chart_color5),
            UIUtils.getColor(R.color.chart_color6),
            UIUtils.getColor(R.color.chart_color7),
    };

    public static View getBarChartView(Context context, Map<String, Double> data, String title) {
        BarChart03View barChart03View = new BarChart03View(context);
        barChart03View.setData(data);
        barChart03View.setTitle(title);
        barChart03View.show();
        return barChart03View;
    }

    public static View getPieChartView(Context context, Map<String, Double> data, String title) {
        PieChart01View barChart03View = new PieChart01View(context);
        barChart03View.setData(data);
        barChart03View.setTitle(title);
        barChart03View.show();
        return barChart03View;
    }

    public static View getLineChartView(Context context, Map<String, Double> data, String title) {
        LineChart02View barChart03View = new LineChart02View(context);
        barChart03View.setData(data);
        barChart03View.setTitle(title);
        barChart03View.show();
        return barChart03View;
    }

    public static int getRandomColor() {
//        Random random = new Random();
//        int r = random.nextInt(255);
//        int g = random.nextInt(255);
//        int b = random.nextInt(255);
//        Log.e("yx", r + "+" + g + "+" + b);
//        return Color.rgb(r, g, b);
        int color = colors[index];
        index++;
        if (index >= colors.length) {
            index = 0;
        }
        return color;
    }

    public static Bitmap loadBitmapFromView(View v) {
        int w = UIUtils.getDimens(R.dimen.x700);
        int h = UIUtils.getDimens(R.dimen.y416);
        Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bmp);
        c.drawColor(Color.WHITE);

//        Drawable drawable = UIUtils.getDrawable(R.drawable.sp_radius_white);
//        Bitmap drawable = BitmapFactory.decodeResource(UIUtils.getResources(), R.drawable.sp_radius_white);
//        c.drawBitmap(drawable, new Matrix(), new Paint());

        /** 如果不设置canvas画布为白色，则生成透明 */
        v.layout(0, 0, w, h);
        v.draw(c);
        return bmp;
    }

}
